package org.example.petitionplatformsystem.service.Impl;

import org.example.petitionplatformsystem.dao.CategoryRepository;
import org.example.petitionplatformsystem.dao.model.Category;
import org.example.petitionplatformsystem.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryRepository categoryRepository;

    @Override
    public void addCategory(Category category) {
        categoryRepository.save(category);
    }

    @Override
    public boolean deleteCategory(long id) {
        categoryRepository.deleteById(id);
        return true;
    }

    @Override
    public void updateCategory(Category category) {
        categoryRepository.save(category);
    }

    @Override
    public Category getCategoryById(long id) {
        return categoryRepository.findById(id).get();
    }

    @Override
    public List<Category> getAllCategories() {
        return categoryRepository.findAll();
    }

    @Override
    public boolean isCategoryExist(String name) {
        System.out.println(categoryRepository.existsByCategoryName(name));
        return !categoryRepository.existsByCategoryName(name);
    }

    @Override
    public Category getCategoryByName(String name) {
        return categoryRepository.findByCategoryName(name);
    }

}
